package com.zhiyin.game.controller;

import com.alibaba.fastjson.JSONObject;
import com.zhiyin.game.Service.GameService;

import com.zhiyin.game.Service.WalkExecuteService;
import com.zhiyin.game.Service.WalkingService;
import com.zhiyin.game.bean.Cell;
import com.zhiyin.game.bean.Vector2;
import com.zhiyin.game.utils.RedisTemplateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.*;
import java.util.HashMap;

@RestController
@RequestMapping
public class GameController {

    @Autowired
    private GameService gameService;

    @Autowired
    private WalkExecuteService walkExecuteService;

    private final String redisKey = "zhiyin.game";

    private Map<String, Object> mapInfo = new HashMap<>();

    @Autowired
    private WalkingService walkingService;

    /**
     * 获取地图数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/getMapInfo", method = {RequestMethod.GET, RequestMethod.POST})
    @CrossOrigin(origins = "*", maxAge = 3600)
    public String getMapInfo(HttpServletRequest request, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Credentials", "true");
        mapInfo.clear();
        Random random = new Random();
        Map resultMap = gameService.initData(10, 10, getBankCount());
        Cell[][] cells = (Cell[][]) resultMap.get("mapInfo");
        for (int i = 0; i < cells.length; i++) {
            Cell[] column = cells[i];
            for (int j = 0; j < column.length; j++) {
                Cell cell = column[j];
                cell.setParentPos(null);
            }
        }
        mapInfo.putAll(resultMap);
        if (RedisTemplateUtils.existHashKey(redisKey, "walk")) {
            RedisTemplateUtils.deleteHashKey(redisKey, "walk");
        }
        RedisTemplateUtils.putHashValue(redisKey, "walk", "0");
        return JSONObject.toJSONString(resultMap);
    }

    private int getBankCount(){
        Random random = new Random();
        int count = random.nextInt(6);
        if (count < 3){
            return getBankCount();
        }
        return count;
    }

    /**
     * 游戏设置
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/gameSetting", method = {RequestMethod.GET, RequestMethod.POST})
    @CrossOrigin(origins = "*", maxAge = 3600)
    public String gameSetting(HttpServletRequest request, HttpServletResponse response, @RequestParam Map params) {
        response.setHeader("Access-Control-Allow-Credentials", "true");
        try {
//            gameService.gameSetting(params);
            return resultSuccessful();
        } catch (Exception e) {
            return resultError(e.toString());
        }

    }

    public String resultSuccessful() {
        Map map = new HashMap();
        map.put("resultcode", 200);
        map.put("successful", 1);
        map.put("error", "");
        return JSONObject.toJSONString(map);
    }

    public String resultError(String error) {
        Map map = new HashMap();
        map.put("resultcode", 200);
        map.put("successful", 0);
        map.put("error", error);
        return JSONObject.toJSONString(map);
    }


    @RequestMapping(value = "/startGame", method = {RequestMethod.GET, RequestMethod.POST})
    @CrossOrigin(origins = "*", maxAge = 3600)
    public String startGame(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setHeader("Access-Control-Allow-Credentials", "true");
        if (RedisTemplateUtils.existHashKey(redisKey, "walk")) {
            RedisTemplateUtils.deleteHashKey(redisKey, "walk");
        }
        RedisTemplateUtils.putHashValue(redisKey, "walk", "1");
        walkExecuteService.doWalk(mapInfo);
        return "ok";
    }

    @RequestMapping(value = "/getWalkList", method = {RequestMethod.GET, RequestMethod.POST})
    @CrossOrigin(origins = "*", maxAge = 3600)
    public String getWalkList(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setHeader("Access-Control-Allow-Credentials", "true");
        List<Vector2> thief = walkingService.startWalking(10, 10, 3, "thief", mapInfo);
        return JSONObject.toJSONString(thief);
    }
}
